package compareDemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Demo1 {

	public static void main(String[] args) {
		User u1=new User("a",1);
		User u2=new User("b",2);
		List<User> list=new ArrayList<User>();
		list.add(u1);
		list.add(u2);
		list.add(new User("a",2));
		list.add(new User("b",1));
		list.add(new User("b",1));
		CompareUser cu=new CompareUser();
		Collections.sort(list,cu);
		for (User user : list) {
			System.out.println(user.getUsername()+"    "+user.getAge());
		}
	}

}
class CompareUser implements Comparator<User>{

	@Override
	public int compare(User o1, User o2) {
		int flag=o1.getAge().compareTo(o2.getAge());
		return flag==0?o1.getUsername().compareTo(o2.getUsername()):flag;
		/*int flag=o1.getAge().compareTo(o2.getAge());
		System.out.println(flag);
		if(flag==0){
			int compareTo = o1.getUsername().compareTo(o2.getUsername());
			System.out.println("==="+compareTo);
			return o1.getUsername().compareTo(o2.getUsername());
		}else{
			return flag;
		}*/
	}
	
}
class User{
	private String username;
	private Integer age;
	public User(String username, Integer age) {
		super();
		this.username = username;
		this.age = age;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
}
